﻿Shader "Nave/FxShader-SliceSahder"
{
	Properties
	{
		_FacadeColor("FacadeColor", Color) = (1,1,1,1)
		_BackColor("BackColor", Color) = (1,1,1,1)
		_Height("Height", Range(-5,5)) = 0.0
	}
	SubShader
	{
		Tags
		{
			"DisableBatching" = "True"
			"Queue" = "Transparent"
			"RenderType" = "Transparent"
		}

		Pass
		{
			Tags {"RenderType" = "Opaque" "Queue" = "Geometry"}

			Zwrite On

			//需要开启双面渲染
			Cull Off
			
			//改善AlphaTest抗锯齿
			AlphaToMask On 

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float3 worldPos : TEXCOORD0;
			};

			float _Height;
			float4 _BackColor, _FacadeColor;
			float4 _SliceObjectPos;

			v2f vert(appdata v)
			{
				v2f o = (v2f)0;
				o.vertex = UnityObjectToClipPos(v.vertex);
				float3 wPos = mul(unity_ObjectToWorld, v.vertex.xyz);

				//_SliceObjectPos是模型的世界空间位置,我们要控制切片范围在box内
				o.worldPos = wPos;// +_SliceObjectPos.xyz;
				return o;
			}

			float4 frag(v2f i, fixed facing : VFACE) : SV_Target
			{
				float4 heightResult = step(i.worldPos.y ,_Height);

				float4 facade = heightResult * _FacadeColor;

				float4 back = heightResult * _BackColor;

				float4 finalColor = 0;

				if (facing > 0) {
					finalColor = facade;
				}
				else {
					finalColor = back;
				}

				clip(finalColor.a - 0.5);
				return finalColor;
			}
			ENDCG
		}
	}
}